{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'axes.linewidth': 1.5,\n",
       " 'axes.unicode_minus': False,\n",
       " 'figure.dpi': 100,\n",
       " 'font.size': 12,\n",
       " 'legend.frameon': True,\n",
       " 'legend.handletextpad': 0.4,\n",
       " 'legend.handlelength': 1,\n",
       " 'legend.facecolor': 'white',\n",
       " 'legend.fancybox': True,\n",
       " 'legend.fontsize': 8,\n",
       " 'mathtext.default': 'regular',\n",
       " 'savefig.bbox': 'tight',\n",
       " 'xtick.labelsize': 10,\n",
       " 'ytick.labelsize': 10,\n",
       " 'xtick.major.size': 4,\n",
       " 'ytick.major.size': 4,\n",
       " 'xtick.major.width': 1,\n",
       " 'ytick.major.width': 1,\n",
       " 'xtick.top': True,\n",
       " 'ytick.right': True,\n",
       " 'axes.edgecolor': 'black',\n",
       " 'savefig.facecolor': 'white',\n",
       " 'axes.facecolor': 'whitesmoke',\n",
       " 'font.family': 'sans',\n",
       " 'font.monospace': 'computer modern roman',\n",
       " 'text.usetex': True,\n",
       " 'axes.grid': True,\n",
       " 'grid.color': 'gray',\n",
       " 'grid.linestyle': '-',\n",
       " 'grid.linewidth': 0.2,\n",
       " 'grid.alpha': 0.3,\n",
       " 'axes.axisbelow': 'line'}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import statsmodels.api as sm\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from visual import setting as se\n",
    "from stargazer.stargazer import Stargazer\n",
    "\n",
    "se.set_rc_params_jup()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "controls = pd.read_csv('US-Leader-Mids.csv')\n",
    "controls.dropna(subset=['gdpc', 'cinc', 'mid_init'], inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "### Smooth out the variation in controls with Logarithmic transformations\n",
    "controls['gdp'] = np.log(controls['gdpc'])\n",
    "controls['cincln'] = np.log(controls['cinc'])\n",
    "controls['warsln'] = np.log(controls['wars'] +1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "## IVs specified\n",
    "ivs = ['distrust', 'task',\n",
    "       'cc',]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def models_(iv, data):\n",
    "\n",
    "    X = data[[iv, 'cincln','gdp', 'warsln', 'time_in']]\n",
    "    y = data[\"mid_init\"] \n",
    "    X = sm.add_constant(X)\n",
    "   \n",
    "    model = sm.OLS(y, X).fit() \n",
    "    predictions = model.predict(X)\n",
    "    print(iv.upper())\n",
    "    print(model.summary())\n",
    "    print(\"\\n\")\n",
    "    print(\"\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# IV with Controls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DISTRUST\n",
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:               mid_init   R-squared:                       0.694\n",
      "Model:                            OLS   Adj. R-squared:                  0.647\n",
      "Method:                 Least Squares   F-statistic:                     14.94\n",
      "Date:                Sat, 30 Oct 2021   Prob (F-statistic):           1.13e-07\n",
      "Time:                        14:55:55   Log-Likelihood:                -101.87\n",
      "No. Observations:                  39   AIC:                             215.7\n",
      "Df Residuals:                      33   BIC:                             225.7\n",
      "Df Model:                           5                                         \n",
      "Covariance Type:            nonrobust                                         \n",
      "==============================================================================\n",
      "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "const        -44.3667     11.366     -3.904      0.000     -67.491     -21.243\n",
      "distrust      28.4010     11.817      2.403      0.022       4.359      52.443\n",
      "cincln        -4.5268      1.390     -3.257      0.003      -7.354      -1.699\n",
      "gdp            2.2629      1.186      1.908      0.065      -0.150       4.676\n",
      "warsln         4.5265      1.113      4.065      0.000       2.261       6.792\n",
      "time_in       -0.2763      0.326     -0.848      0.402      -0.939       0.387\n",
      "==============================================================================\n",
      "Omnibus:                        5.516   Durbin-Watson:                   1.899\n",
      "Prob(Omnibus):                  0.063   Jarque-Bera (JB):                4.119\n",
      "Skew:                           0.664   Prob(JB):                        0.128\n",
      "Kurtosis:                       3.879   Cond. No.                         289.\n",
      "==============================================================================\n",
      "\n",
      "Warnings:\n",
      "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "TASK\n",
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:               mid_init   R-squared:                       0.690\n",
      "Model:                            OLS   Adj. R-squared:                  0.644\n",
      "Method:                 Least Squares   F-statistic:                     14.72\n",
      "Date:                Sat, 30 Oct 2021   Prob (F-statistic):           1.32e-07\n",
      "Time:                        14:55:55   Log-Likelihood:                -102.07\n",
      "No. Observations:                  39   AIC:                             216.1\n",
      "Df Residuals:                      33   BIC:                             226.1\n",
      "Df Model:                           5                                         \n",
      "Covariance Type:            nonrobust                                         \n",
      "==============================================================================\n",
      "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "const          8.2649     18.504      0.447      0.658     -29.382      45.912\n",
      "task         -27.9169     12.029     -2.321      0.027     -52.391      -3.443\n",
      "cincln        -2.6461      1.457     -1.816      0.078      -5.611       0.319\n",
      "gdp           -0.4819      1.406     -0.343      0.734      -3.343       2.379\n",
      "warsln         3.8570      1.059      3.642      0.001       1.702       6.012\n",
      "time_in        0.0150      0.290      0.052      0.959      -0.574       0.604\n",
      "==============================================================================\n",
      "Omnibus:                        1.751   Durbin-Watson:                   2.066\n",
      "Prob(Omnibus):                  0.417   Jarque-Bera (JB):                0.803\n",
      "Skew:                           0.133   Prob(JB):                        0.669\n",
      "Kurtosis:                       3.651   Cond. No.                         424.\n",
      "==============================================================================\n",
      "\n",
      "Warnings:\n",
      "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "CC\n",
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:               mid_init   R-squared:                       0.667\n",
      "Model:                            OLS   Adj. R-squared:                  0.616\n",
      "Method:                 Least Squares   F-statistic:                     13.19\n",
      "Date:                Sat, 30 Oct 2021   Prob (F-statistic):           4.31e-07\n",
      "Time:                        14:55:55   Log-Likelihood:                -103.52\n",
      "No. Observations:                  39   AIC:                             219.0\n",
      "Df Residuals:                      33   BIC:                             229.0\n",
      "Df Model:                           5                                         \n",
      "Covariance Type:            nonrobust                                         \n",
      "==============================================================================\n",
      "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "const        -11.7031     14.327     -0.817      0.420     -40.851      17.445\n",
      "cc           -36.5270     22.520     -1.622      0.114     -82.345       9.291\n",
      "cincln        -4.6864      1.514     -3.096      0.004      -7.766      -1.607\n",
      "gdp            1.8302      1.209      1.514      0.139      -0.629       4.289\n",
      "warsln         3.5011      1.097      3.190      0.003       1.268       5.734\n",
      "time_in       -0.0016      0.305     -0.005      0.996      -0.621       0.618\n",
      "==============================================================================\n",
      "Omnibus:                        1.650   Durbin-Watson:                   2.134\n",
      "Prob(Omnibus):                  0.438   Jarque-Bera (JB):                0.795\n",
      "Skew:                           0.293   Prob(JB):                        0.672\n",
      "Kurtosis:                       3.382   Cond. No.                         480.\n",
      "==============================================================================\n",
      "\n",
      "Warnings:\n",
      "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
      "\n",
      "\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for iv in ivs:\n",
    "    \n",
    "    models_(iv, controls)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<table style=\"text-align:center\"><tr><td colspan=\"4\" style=\"border-bottom: 1px solid black\"></td></tr><tr><td style=\"text-align:left\"></td><td colspan=\"3\"><em>Dependent variable:</em></td></tr><tr><td style=\"text-align:left\"></td><tr><td style=\"text-align:left\"></td><td>(1)</td><td>(2)</td><td>(3)</td></tr><tr><td colspan=\"4\" style=\"border-bottom: 1px solid black\"></td></tr><tr><td style=\"text-align:left\">Distrust</td><td>28.401<sup>**</sup></td><td></td><td></td></tr><tr><td style=\"text-align:left\"></td><td>(11.817)</td><td></td><td></td></tr><tr><td style=\"text-align:left\">Task</td><td></td><td>-27.917<sup>**</sup></td><td></td></tr><tr><td style=\"text-align:left\"></td><td></td><td>(12.029)</td><td></td></tr><tr><td style=\"text-align:left\">Conceptual Complexity</td><td></td><td></td><td>-36.527<sup></sup></td></tr><tr><td style=\"text-align:left\"></td><td></td><td></td><td>(22.52)</td></tr><tr><td style=\"text-align:left\">CINC</td><td>-4.527<sup>***</sup></td><td>-2.646<sup>*</sup></td><td>-4.686<sup>***</sup></td></tr><tr><td style=\"text-align:left\"></td><td>(1.39)</td><td>(1.457)</td><td>(1.514)</td></tr><tr><td style=\"text-align:left\">GDP per capita</td><td>2.263<sup>*</sup></td><td>-0.482<sup></sup></td><td>1.83<sup></sup></td></tr><tr><td style=\"text-align:left\"></td><td>(1.186)</td><td>(1.406)</td><td>(1.209)</td></tr><tr><td style=\"text-align:left\">MIDs in System</td><td>4.526<sup>***</sup></td><td>3.857<sup>***</sup></td><td>3.501<sup>***</sup></td></tr><tr><td style=\"text-align:left\"></td><td>(1.113)</td><td>(1.059)</td><td>(1.097)</td></tr><tr><td style=\"text-align:left\">Years as Pres</td><td>-0.276<sup></sup></td><td>0.015<sup></sup></td><td>-0.002<sup></sup></td></tr><tr><td style=\"text-align:left\"></td><td>(0.326)</td><td>(0.29)</td><td>(0.305)</td></tr><tr><td style=\"text-align:left\">Constant</td><td>-44.367<sup>***</sup></td><td>8.265<sup></sup></td><td>-11.703<sup></sup></td></tr><tr><td style=\"text-align:left\"></td><td>(11.366)</td><td>(18.504)</td><td>(14.327)</td></tr><td colspan=\"4\" style=\"border-bottom: 1px solid black\"></td></tr><tr><td style=\"text-align: left\">Observations</td><td>39.0</td><td>39.0</td><td>39.0</td></tr><tr><td style=\"text-align: left\">R<sup>2</sup></td><td>0.694</td><td>0.69</td><td>0.667</td></tr><tr><td style=\"text-align: left\">Adjusted R<sup>2</sup></td><td>0.647</td><td>0.644</td><td>0.616</td></tr><tr><td style=\"text-align: left\">Residual Std. Error</td><td>3.585</td><td>3.603</td><td>3.74</td></tr><tr><td style=\"text-align: left\">F Statistic</td><td>14.94<sup>***</sup></td><td>14.723<sup>***</sup></td><td>13.192<sup>***</sup></td></tr><tr><td colspan=\"4\" style=\"border-bottom: 1px solid black\"></td></tr><tr><td style=\"text-align: left\">Note:</td>\n",
      " <td colspan=\"3\" style=\"text-align: right\">\n",
      "  <sup>*</sup>p&lt;0.1;\n",
      "  <sup>**</sup>p&lt;0.05;\n",
      "  <sup>***</sup>p&lt;0.01\n",
      " </td></tr></table>\n"
     ]
    }
   ],
   "source": [
    "models_t = []\n",
    "data = controls\n",
    "for iv in ivs:\n",
    "\n",
    "    y = data[\"mid_init\"] \n",
    "    X = data[[iv, 'cincln','gdp', 'warsln', 'time_in']]\n",
    "    X = sm.add_constant(X)\n",
    "\n",
    "    model = sm.OLS(y, X).fit() \n",
    "    models_t.append(model)\n",
    "\n",
    "    \n",
    "starg = Stargazer(models_t)\n",
    "\n",
    "starg.covariate_order(['distrust', 'task', 'cc','cincln','gdp', 'warsln', 'time_in','const'])\n",
    "starg.rename_covariates({'distrust': 'Distrust', 'task':'Task', 'cc':'Conceptual Complexity',\n",
    "                         'cincln':'CINC','gdp':'GDP per capita', 'warsln': 'MIDs in System', \n",
    "                         'time_in': 'Years as Pres', \n",
    "                         'const':'Constant'})\n",
    "\n",
    "starg.show_degrees_of_freedom(False)\n",
    "\n",
    "print(starg.render_html())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<table style=\"text-align:center\"><tr><td colspan=\"4\" style=\"border-bottom: 1px solid black\"></td></tr><tr><td style=\"text-align:left\"></td><td colspan=\"3\"><em>Dependent variable:</em></td></tr><tr><td style=\"text-align:left\"></td><tr><td style=\"text-align:left\"></td><td>(1)</td><td>(2)</td><td>(3)</td></tr><tr><td colspan=\"4\" style=\"border-bottom: 1px solid black\"></td></tr><tr><td style=\"text-align:left\">Distrust</td><td>28.401<sup>**</sup></td><td></td><td></td></tr><tr><td style=\"text-align:left\"></td><td>(11.817)</td><td></td><td></td></tr><tr><td style=\"text-align:left\">Task</td><td></td><td>-27.917<sup>**</sup></td><td></td></tr><tr><td style=\"text-align:left\"></td><td></td><td>(12.029)</td><td></td></tr><tr><td style=\"text-align:left\">Conceptual Complexity</td><td></td><td></td><td>-36.527<sup></sup></td></tr><tr><td style=\"text-align:left\"></td><td></td><td></td><td>(22.52)</td></tr><tr><td style=\"text-align:left\">CINC</td><td>-4.527<sup>***</sup></td><td>-2.646<sup>*</sup></td><td>-4.686<sup>***</sup></td></tr><tr><td style=\"text-align:left\"></td><td>(1.39)</td><td>(1.457)</td><td>(1.514)</td></tr><tr><td style=\"text-align:left\">GDP per capita</td><td>2.263<sup>*</sup></td><td>-0.482<sup></sup></td><td>1.83<sup></sup></td></tr><tr><td style=\"text-align:left\"></td><td>(1.186)</td><td>(1.406)</td><td>(1.209)</td></tr><tr><td style=\"text-align:left\">MIDs in System</td><td>4.526<sup>***</sup></td><td>3.857<sup>***</sup></td><td>3.501<sup>***</sup></td></tr><tr><td style=\"text-align:left\"></td><td>(1.113)</td><td>(1.059)</td><td>(1.097)</td></tr><tr><td style=\"text-align:left\">Years as Pres</td><td>-0.276<sup></sup></td><td>0.015<sup></sup></td><td>-0.002<sup></sup></td></tr><tr><td style=\"text-align:left\"></td><td>(0.326)</td><td>(0.29)</td><td>(0.305)</td></tr><tr><td style=\"text-align:left\">Constant</td><td>-44.367<sup>***</sup></td><td>8.265<sup></sup></td><td>-11.703<sup></sup></td></tr><tr><td style=\"text-align:left\"></td><td>(11.366)</td><td>(18.504)</td><td>(14.327)</td></tr><td colspan=\"4\" style=\"border-bottom: 1px solid black\"></td></tr><tr><td style=\"text-align: left\">Observations</td><td>39.0</td><td>39.0</td><td>39.0</td></tr><tr><td style=\"text-align: left\">R<sup>2</sup></td><td>0.694</td><td>0.69</td><td>0.667</td></tr><tr><td style=\"text-align: left\">Adjusted R<sup>2</sup></td><td>0.647</td><td>0.644</td><td>0.616</td></tr><tr><td style=\"text-align: left\">Residual Std. Error</td><td>3.585</td><td>3.603</td><td>3.74</td></tr><tr><td style=\"text-align: left\">F Statistic</td><td>14.94<sup>***</sup></td><td>14.723<sup>***</sup></td><td>13.192<sup>***</sup></td></tr><tr><td colspan=\"4\" style=\"border-bottom: 1px solid black\"></td></tr><tr><td style=\"text-align: left\">Note:</td>\n",
    " <td colspan=\"3\" style=\"text-align: right\">\n",
    "  <sup>*</sup>p&lt;0.1;\n",
    "  <sup>**</sup>p&lt;0.05;\n",
    "  <sup>***</sup>p&lt;0.01\n",
    " </td></tr></table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
